package com.example.cms.web.Controller;

import com.example.cms.Service.IUserService;
import com.example.common.Utils.PageUtil;
import com.example.common.web.response.Result;
import com.example.user.bean.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/11/1-11-01-15:57
 * @Description：com.example.cms.web.Controller
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/auth/user")
public class UserController {

    @Autowired
    private IUserService service;

    @ApiOperation(value = "根据id删除用户",notes = "删除")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "id",value = "用户id",dataType = "int",paramType = "query",required = true)
    )
    @GetMapping("/delete")
    public Result deleteUserById(@RequestParam("id") int id){
        service.deleteById(id);
        return Result.success("删除用户成功");
    }

    @ApiOperation(value = "查询当前用户的基本信息",notes = "通过用户名查询")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "query",required = true)
    )
    @GetMapping("/info")
    public Result GetUserByName(@RequestParam("username") String username){
        User user = service.getUserByName(username);
        return Result.success(user);
    }

    @ApiOperation(value = "根据角色名分页查询所有用户",notes = "通过role角色名查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role",value = "角色名",dataType = "int",paramType = "query"),
//            @ApiImplicitParam(name = "pageUtil",value = "分页信息",dataType = "json",paramType = "body"),
    })
    @PostMapping("/page/query")
    public Result GetAllUserByRole(Integer role,@RequestBody PageUtil pageUtil){

        PageUtil pageUtil1 = service.pagesOfUsers(role, pageUtil);
        return Result.success(pageUtil1);
    }

    @ApiOperation(value = "通过id更新用户的信息",notes = "根据需要更新信息")
    @PutMapping("/update")
    public Result UpdateUser(@RequestBody User user){
        service.updateUserById(user);
        return Result.success("成功更新用户信息！");
    }

    @ApiOperation(value = "通过id更新用户头像",notes = "头像图片地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "icon",value = "图片地址",dataType = "String",paramType = "query"),
    })
    @PutMapping("/updateUserIcon")
    public Result UpdateUserIcon(@RequestParam("id") int id, @RequestParam("icon")String icon){
        service.updateIconById(id,icon);
        return Result.success("成功更新用户头像！");
    }


}
